package com.example.mybatis_demo.controller;


import com.example.mybatis_demo.component.JwtResponse;
import com.example.mybatis_demo.entity.UserEntity;
import com.example.mybatis_demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getall")
    public List<UserEntity> getAll() {
        log.error("======使用lombok打印日志=========");
        return userService.getAll();
    }

    @GetMapping("login")
    public JwtResponse login(@RequestParam String username, @RequestParam String password) throws Exception {
        String jwt = "";
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        System.out.println(userEntity);
        try {
            jwt = userService.login(userEntity);
            System.out.println(jwt);
            return JwtResponse.success(jwt);
        } catch (Exception e) {
            e.printStackTrace();
            return JwtResponse.fail(jwt);
        }
    }
}
